package com.fdb.efp.cus.api.rest;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.cus.service.facade.CusIndivRelService;
import com.fdb.efp.cus.service.vo.CusIndivRelVO;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class CusIndivRelController extends AbstractController {

	@Autowired
	@Qualifier("cusIndivRelService")
	private CusIndivRelService cusIndivRelService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/rels", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<CusIndivRelVO>> queryCusIndivRelAll(@RequestBody CusIndivRelVO vo) {
		setUserInfoToVO(vo);
		// 根据用户角色获取用户权限方案
		List<CusIndivRelVO> outputVo = null;

		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = cusIndivRelService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = cusIndivRelService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = cusIndivRelService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = cusIndivRelService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/rel/", method = RequestMethod.GET)
	public @ResponseBody ResponseData<CusIndivRelVO> queryByPk() {
		CusIndivRelVO inVo = new CusIndivRelVO();
		CusIndivRelVO outputVo = cusIndivRelService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param cusIndivRel
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/rel", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CusIndivRelVO inCusIndivRelVo) {
		int outputVo = cusIndivRelService.deleteByPk(inCusIndivRelVo);

		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inCusIndivRelVo
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/rel", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CusIndivRelVO inCusIndivRelVo) {
		int outputVo = cusIndivRelService.updateByPk(inCusIndivRelVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inCusIndivRelVo
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/rel", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCusIndivRel(@RequestBody CusIndivRelVO inCusIndivRelVo) {
		int outputVo = cusIndivRelService.insertCusIndivRel(inCusIndivRelVo);
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		if (outputVo == -2) {
			responseData.setRows(outputVo);
			responseData.setMessage("操作失败,未填写联系人姓名!");
			responseData.setCode(outputVo + "");
			responseData.setSuccess(false);
			return responseData;
		}
		if (outputVo == -3) {
			responseData.setRows(outputVo);
			responseData.setMessage("操作失败,未填写与联系人关系!");
			responseData.setCode(outputVo + "");
			responseData.setSuccess(false);
			return responseData;
		}
		if (outputVo == -4) {
			responseData.setRows(outputVo);
			responseData.setMessage("操作失败,未填写联系人移动电话!");
			responseData.setCode(outputVo + "");
			responseData.setSuccess(false);
			return responseData;
		}
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键批量删除信息
	 *
	 * @param cusIndivRel
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/rels", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> batchDeleteByPk(@RequestBody CusIndivRelVO inCusIndivRelVo) {
		int outputVo = cusIndivRelService.batchDeleteByPk(inCusIndivRelVo);

		return getResponseData(outputVo);

	}
}
